\documentclass[10pt,a4paper]{article} 

\usepackage{ctex} % 中文支持
\usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry} % 页边距
\usepackage{amsmath, amssymb} % 数学公式与符号
\usepackage{graphicx}

\usepackage{pythonhighlight}
\usepackage{url} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 使表格美观
\usepackage{array}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
%\newcolumntype{N}{@{}m{0pt}@{}}
\setlength\extrarowheight{3pt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{titling}
\setlength{\droptitle}{-2cm} % 标题上移 

%%文档的题目、作者与日期
\author{五六七 }
\title{国土面积与样条插值 }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\maketitle

\begin{abstract}
已知某国家边界上一些点的坐标，计算国土面积。
\end{abstract}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\setcounter{tocdepth}{2}
%\renewcommand\contentsname{目录}
%
%\renewcommand {\baselinestretch} {1.3}\normalsize 
%\tableofcontents 
%\renewcommand {\baselinestretch} {1.0}\normalsize


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{问题描述}
为了计算一个国家的国土面积，先做了如下测量。
取定地图左下角的一个点作为原点，由西向东作为 $x$ 轴，由南向北作为 $y$ 轴。
将 $x$ 轴划分为一些小区间，测出每个分点的坐标。
对 $x$ 轴的每个分点，画一条竖线，测出国土上南北两端的 $y$ 坐标。
从这些测量数据，估计国土的面积。

\begin{table}[ht]\centering
\caption{国土边界测量点的坐标 }\vspace{0.2cm}
\begin{tabular}{|M{0.8cm}|c|c|c|c|c|c|c|c|c|} \hline 
$x$ & 7.0 & 10.5 & 13.0 & 17.5 & 34.0 & 40.5 & 44.5 & 48.0 & 56.0  \\ \hline 
$y_1$ & 44 & 45 & 47 & 50 & 50 & 38 & 30 & 30 & 34  \\ \hline 
$y_2$ & 44 & 59 & 70 & 72 & 93 & 100 & 110 & 110 & 110  \\ \hline \hline 
$x$ & 61.0 & 68.5 & 76.5 & 80.5 & 91.0 & 96.0 & 101.0 & 104.0 & 106.5  \\ \hline 
$y_1$ & 36 & 34 & 41 & 45 & 46 & 43 & 37 & 33 & 28  \\ \hline 
$y_2$ & 117 & 118 & 116 & 118 & 118 & 121 & 124 & 121 & 121  \\ \hline \hline 
$x$ & 111.5 & 118.0 & 123.5 & 136.5 & 142.0 & 146.0 & 150.0 & 157.0 & 158.0  \\ \hline 
$y_1$ & 32 & 65 & 55 & 54 & 52 & 50 & 66 & 66 & 68  \\ \hline 
$y_2$ & 121 & 122 & 116 & 83 & 81 & 82 & 86 & 85 & 68  \\ \hline 
\end{tabular}
\end{table}

地图测量单位：毫米。比例尺：地图 18 毫米 = 实际 40 千米。


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{建立模型}
首先在 $xy$ 坐标系中画出这些测量点的位置。
可见横坐标的最小值和最大值分别是 $a=7.0$ 和 $b=158.0$.  
将国土边界分成上下两段，看作两个函数 $y= g(x)$ 和 $y=f(x)$ 的图像。 

\begin{figure}[ht!]\centering
\includegraphics [height=4cm, width=7cm]{country_boundary.png}
\caption{线性插值的国土边界 }
\end{figure}

于是国土面积可以由下述定积分来计算，
\begin{eqnarray}
A=\int_a^b (g(x)-f(x))dx.
\end{eqnarray}

\subsection{线性插值计算}
对横坐标相邻的两个点，我们用折线代替边界线。
也就是说，我们用线性插值来估计函数 $f(x)$ 与 $g(x)$ 的表达式。
因为测量时，所取的点，每个横坐标都测量了上下两个纵坐标，所以总面积可以写成很多细长的梯形的面积的和。

\begin{figure}[ht]\centering
\includegraphics [height=4cm, width=7cm]{country_boundary_trapezoids.png}
\caption{国土面积近似为很多长条梯形的面积的和 }
\end{figure}

\subsection{三次样条插值计算}
设有横坐标相邻的三个点 $A, B, C$. 用折线连接，使用的是一次多项式。
如果我们用一个三次多项式来连接 $A$ 与 $B$, 用另一个三次多项式来连接 $B$ 与 $C$, 
并且使得这两个三次多项式在 $B$ 点有相同的一阶导数与二阶导数，即有相同的切线和曲率，那么这就是三次样条插值。

\begin{figure}[ht]\centering
\includegraphics [height=4cm, width=7cm]{country_boundary_cubic.png}
\caption{三次样条插值的国土边界 }
\end{figure}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{编程计算}

首先载入数值计算和函数画图的模块。
\begin{python}
import numpy as np
import matplotlib.pyplot as plt
\end{python}

输入测量点的横坐标数据，和上下边界的两个纵坐标数据。
\begin{python}
x=np.array([7.0, 10.5, 13.0, 17.5, 34.0, 40.5, 44.5, 48.0, 56.0,
            61.0, 68.5, 76.5, 80.5, 91.0, 96.0, 101.0, 104.0, 106.5, 
            111.5, 118.0, 123.5, 136.5, 142.0, 146.0, 150.0, 157.0, 158.0])

y1=np.array([44, 45, 47, 50, 50, 38, 30, 30, 34, 
             36, 34, 41, 45, 46, 43, 37, 33, 28, 
             32, 65, 55, 54, 52, 50, 66, 66, 68])

y2=np.array([44, 59, 70, 72, 93, 100, 110, 110, 110,
             117, 118, 116, 118, 118, 121, 124, 121, 121, 
             121, 122, 116, 83, 81, 82, 86, 85, 68])
\end{python}

画出上下两个函数的散点和折线图。
\begin{python}
plt.plot(x,y1,'.-',label='y=f(x)')
plt.plot(x,y2,'.-',label='y=g(x)')
plt.legend(loc='upper left')
\end{python}

\subsection{线性插值计算}

对每个所取的横坐标，画出从最南端到最北端的连线。
\begin{python}
plt.plot(x,y1,'.-')
plt.plot(x,y2,'.-')
plt.plot([x,x],[y1,y2])
\end{python}

计算每个长条梯形的面积，累加成为地图上的面积。
\begin{python}
N=len(x)
Area=0
for k in range(N-1):
    LeftSide = y2[k]-y1[k]
    RightSide = y2[k+1]-y1[k+1]
    dx = x[k+1]-x[k]
    Area = Area + (LeftSide + RightSide)*dx/2
\end{python}

根据地图上的18毫米相当于实际的40公里，从地图面积计算国土面积。
\begin{python}
RealArea=Area*(40/18)**2
print('The area by linear interpolation is %.2f square kilometers.'%RealArea)
\end{python}


\subsection{三次样条插值计算}

载入scipy 模块的一维插值函数。
\begin{python}
from scipy.interpolate import interp1d
\end{python}

自变量取值区间的左右端点，设置自变量的更多取值点。
\begin{python}
a=np.min(x)
b=np.max(x)
n=len(x)*5
xnew=np.linspace(a,b,n)
\end{python}

使用一维数据的三次样条插值函数 \texttt{interp1d}, 用已知数据，计算上下两条边界的函数，然后计算在自变量的更多取值点上的函数值。
\begin{python}
f1new=interp1d(x,y1,'cubic')
y1new=f1new(xnew)
f2new=interp1d(x,y2,'cubic')
y2new=f2new(xnew)
\end{python}

画出三次样条插值得到的两个函数图像。可见函数图像变得光滑了。
\begin{python}
plt.plot(x,y1,'.')
plt.plot(x,y2,'.')
plt.plot(xnew,y1new,'-')
plt.plot(xnew,y2new,'-')
\end{python}

使用三次样条插值数据计算面积。
\begin{python}
VertBars=y2new-y1new
SpArea=np.sum((VertBars[:-1]+VertBars[1:])*np.diff(xnew)/2)
\end{python}

根据比例尺，从地图面积换算成国土面积。
\begin{python}
RealSpArea=SpArea*(40/18)**2
print('The area by spline interpolation is %.2f square kilometers.'%RealSpArea)
\end{python}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{回答问题}
将未测量的边界坐标用折线来计算，可得国土面积的近似值为 42413.58 平方公里。
另一种方法，将上下边界的两个函数用三次样条插值来计算，可得国土面积的近似值为 42454.76 平方公里。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\section{参考文献 }
\begin{thebibliography}{99}
\bibitem{sishoukui-2} 司守奎,孙玺菁. \emph{Python数学建模算法与应用}, 国防工业出版社. 2022年1月第1版. 


\end{thebibliography}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

